<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace Common\Api;
define("cmToInch", 0.393700787);
define("InchToCm", 2.54);
define("kgToPound", 2.2046228);
class CommissionApi {

    private $amzMarketSearchUrls=array(
        array('cn'=>'美国', 'name'=>'US'),
        array('cn'=>'英国', 'name'=>'UK'),
        array('cn'=>'德国', 'name'=>'DE'),
        array('cn'=>'法国', 'name'=>'FR')
    );

    private $rates;

    private $commissionRate=array(
        array('name'=>'6%', 'val'=>'0.06'),
        array('name'=>'8%', 'val'=>'0.08'),
        array('name'=>'12%', 'val'=>'0.12'),
        array('name'=>'15%', 'val'=>'0.15'),
        array('name'=>'20%', 'val'=>'0.20'),
        array('name'=>'45%', 'val'=>'0.45')
    );

    private $usWeightHandling=array(
        "ss1" => 0.5,
        "ls1" => 0.96,
        "ls1to2" => 1.95

    );

    /*
    $usWeightHandling["ss1"]=0.5;
    $usWeightHandling["ls1"]=0.96;
    $usWeightHandling["ls1to2"]=1.95;*/

    private $marketOrderHandling=array(
        "US" => 1,
        "UK" => 0,
        "DE" => 0,
        "FR" => 0

    );

    private $marketpickNpack=array(
        "US" => 1.06,
        "UK" => 0,
        "DE" => 0,
        "FR" => 0
    );

    /*
    $marketOrderHandling["US"]=1;
    $marketOrderHandling["UK"]=0;
    $marketOrderHandling["DE"]=0;
    $marketOrderHandling["FR"]=0;


    private $marketpickNpack=array();
    $marketpickNpack["US"]=1.06;
    $marketpickNpack["UK"]=0;
    $marketpickNpack["DE"]=0;
    $marketpickNpack["FR"]=0;
    */

    private $usWeightHandlingAbove2=0.39;

    //statr edit xiaojing  2016-05-07

//    private $usPackingWeightSS = 0.25 /kgToPound;

    private $usPackingWeightSS = 0.25;

//    private $usPackingWeightAbove1 = 0.25 /kgToPound;

    private $usPackingWeightAbove1 = 0.25;

    //end edit xiaojing  2016-05-07


    private $ssLongest = 15 * InchToCm ;

    private $ssMedian = 12 * InchToCm ;

    private $ssShortest = 0.75 * InchToCm ;

    //statr edit xiajing 2015-05-07

//    private $ssWeight = 14/16 / kgToPound;

    private $ssWeight = 0.75;

    //end edit xiajing 2015-05-07

// 美国 standard size 标准
    private $lsLongest = 18 * InchToCm ; //最长边

    private $lsMedian = 14 * InchToCm ;  //中间边

    private $lsShortest = 8 * InchToCm ; //最短边

    //statr edit xiajing 2015-05-07

//    private $lsWeight = 20 / kgToPound; //重量等于或少于的重量

    private $lsWeight = 20; //重量等于或少于的重量

    //end edit xiajing 2015-05-07

//length,height,width,min weight, max weight
    private $ukWeightHandling=array(
        array('length'=>20,'height'=>15,'width'=>1,'packingWeight'=>0.02,'minWeight'=>0,'maxWeight'=>0.1,'cost'=>1.07),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0,'maxWeight'=>0.1,'cost'=>1.19),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0.1,'maxWeight'=>0.25,'cost'=>1.31),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0.25,'maxWeight'=>0.5,'cost'=>1.51),
        array('length'=>33,'height'=>23,'width'=>5,'packingWeight'=>0.04,'minWeight'=>0,'maxWeight'=>1,'cost'=>1.7),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0,'maxWeight'=>0.25,'cost'=>1.66),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0.25,'maxWeight'=>0.5,'cost'=>1.72),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0.5,'maxWeight'=>1,'cost'=>1.77),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>1,'maxWeight'=>1.5,'cost'=>2.19),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>1.5,'maxWeight'=>2,'cost'=>2.41),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>2,'maxWeight'=>3,'cost'=>3.30),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>3,'maxWeight'=>4,'cost'=>3.40),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>4,'maxWeight'=>5,'cost'=>3.40),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>5,'maxWeight'=>6,'cost'=>3.45),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>6,'maxWeight'=>7,'cost'=>3.45),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>7,'maxWeight'=>8,'cost'=>3.53),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>8,'maxWeight'=>9,'cost'=>3.53),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>9,'maxWeight'=>10,'cost'=>3.53),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>10,'maxWeight'=>11,'cost'=>3.53),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>11,'maxWeight'=>12,'cost'=>3.54)

    );

//length,height,width,min weight, max weight
    private $deWeightHandling=array(
        array('length'=>20,'height'=>15,'width'=>1,'packingWeight'=>0.02,'minWeight'=>0,'maxWeight'=>0.1,'cost'=>1.6),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0,'maxWeight'=>0.1,'cost'=>1.72),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0.1,'maxWeight'=>0.25,'cost'=>1.73),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0.25,'maxWeight'=>0.5,'cost'=>1.77),
        array('length'=>33,'height'=>23,'width'=>5,'packingWeight'=>0.04,'minWeight'=>0,'maxWeight'=>1,'cost'=>2.14),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0,'maxWeight'=>0.25,'cost'=>2.32),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0.25,'maxWeight'=>0.5,'cost'=>2.41),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0.5,'maxWeight'=>1,'cost'=>2.98),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>1,'maxWeight'=>1.5,'cost'=>3.04),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>1.5,'maxWeight'=>2,'cost'=>3.10),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>2,'maxWeight'=>3,'cost'=>4.18),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>3,'maxWeight'=>4,'cost'=>4.19),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>4,'maxWeight'=>5,'cost'=>4.19),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>5,'maxWeight'=>6,'cost'=>4.27),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>6,'maxWeight'=>7,'cost'=>4.27),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>7,'maxWeight'=>8,'cost'=>4.40),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>8,'maxWeight'=>9,'cost'=>4.40),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>9,'maxWeight'=>10,'cost'=>4.40),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>10,'maxWeight'=>11,'cost'=>4.40),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>11,'maxWeight'=>12,'cost'=>4.41)
    );

    private $frWeightHandling=array(
        array('length'=>20,'height'=>15,'width'=>1,'packingWeight'=>0.02,'minWeight'=>0,'maxWeight'=>0.1,'cost'=>1.98),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0,'maxWeight'=>0.1,'cost'=>2.08),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0.1,'maxWeight'=>0.25,'cost'=>2.08),
        array('length'=>33,'height'=>23,'width'=>2.5,'packingWeight'=>0.04,'minWeight'=>0.25,'maxWeight'=>0.5,'cost'=>2.24),
        array('length'=>33,'height'=>23,'width'=>5,'packingWeight'=>0.04,'minWeight'=>0,'maxWeight'=>1,'cost'=>2.38),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0,'maxWeight'=>0.25,'cost'=>2.55),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0.25,'maxWeight'=>0.5,'cost'=>2.74),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>0.5,'maxWeight'=>1,'cost'=>3.50),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>1,'maxWeight'=>1.5,'cost'=>3.70),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>1.5,'maxWeight'=>2,'cost'=>3.83),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>2,'maxWeight'=>3,'cost'=>4.98),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>3,'maxWeight'=>4,'cost'=>5.14),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>4,'maxWeight'=>5,'cost'=>5.14),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>5,'maxWeight'=>6,'cost'=>5.22),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>6,'maxWeight'=>7,'cost'=>5.22),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>7,'maxWeight'=>8,'cost'=>5.35),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>8,'maxWeight'=>9,'cost'=>5.35),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>9,'maxWeight'=>10,'cost'=>5.35),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>10,'maxWeight'=>11,'cost'=>5.35),
        array('length'=>45,'height'=>34,'width'=>26,'packingWeight'=>0.1,'minWeight'=>11,'maxWeight'=>12,'cost'=>5.36)
    );




    /*
    var $firstRender = true;
    var $listRenderHold = LaunchScreen.hold();
    listFadeInHold = null;

    Template.commission.onCreated(function(){


        var $instance = this;
        this.cost = new ReactiveVar();
        this.price = new ReactiveVar();
        this.commission = new ReactiveVar();
        this.market = new ReactiveVar();
        this.priceInRMB = new ReactiveVar();
        this.priceCurrency=new ReactiveVar("USD");
        this.calcCurrency=new ReactiveVar("USD");
        this.currencyRate=new ReactiveVar();
        this.currencyRateDate=new ReactiveVar();
        this.freightCost = new ReactiveVar();
        this.weightActual = new ReactiveVar();
        this.productHeight = new ReactiveVar();
        this.productWidth = new ReactiveVar();
        this.productLength = new ReactiveVar();
        this.orderHandling = new ReactiveVar();
        this.pickNPack = new ReactiveVar();
        var $cr = this.currencyRate;
        var $cd = this.currencyRateDate;
        Meteor.call('getCurrency',function(err,result){
            rates = result;
            cr.set(rates.USD.toFixed(4));
            cd.set(moment(rates.createdAt).format('YYYY MM DD h:mm a'));
        });

        this.finalCost = new ReactiveVar();
        this.finalShipping = new ReactiveVar();
        this.finalCommission = new ReactiveVar();
        this.finalFBA = new ReactiveVar();
        this.finalPrice = new ReactiveVar();
        this.finalMargin = new ReactiveVar();
    });

    Template.commission.onRendered(function() {


        this.commission.set($("#amzCommissionRate option:selected" ).val());
        this.market.set($("#amzMarket option:selected" ).val());
        this.orderHandling.set(marketOrderHandling["US"]);
        this.pickNPack.set(marketpickNpack["US"]);
    });
    */
    private $standsize;
    private $commission ;
    private $cost ;
    private $price ;
    private $market ;
    private $priceInRMB ;
    private $priceCurrency;
    private $calcCurrency;
    private $currencyRate;
    private $currencyRateDate;
    private $freightCost ;
    private $weightActual ;
    private $productHeight ;
    private $productWidth ;
    private $productLength ;
    private $orderHandling ;
    private $pickNPack ;
    private $finalCost ;
    private $finalShipping ;
    private $finalCommission ;
    private $finalFBA ;
    private $finalPrice ;
    private $finalMargin ;
    private $MarginPercent ; //毛利率

    function commission()
    {
        //初始化值放到这里面

        $cr = $this->currencyRate;
        $cd = $this->currencyRateDate;

        $this->currencyRate = $this->rates;
        $this->currencyRateDate = date("Y-m-d H:i:s");//date_format(time(),"YYYY MM DD h:mm a");

        /*
        Meteor.call('getCurrency',function(err,result){
            rates = result;
            cr.set(rates.USD.toFixed(4));
            cd.set(moment(rates.createdAt).format('YYYY MM DD h:mm a'));
        });*/


    }

    function isStandard($height,$length,$width,$weight)
    {
        if($weight > $this->ssWeight){
            return false;
        }
        $measures = array($height, $length, $width);
        sort($measures);
        //measures.sort(function($a, $b){return $a-$b});
        if($measures[2]> $this->ssLongest){
            return false;
        }
        if($measures[1]> $this->ssMedian){
            return false;
        }
        if($measures[0]> $this->ssShortest){
            return false;
        }
        return true;
    }

    function isLargeStandard ($height,$length,$width,$weight)
    {
        if($weight > $this->lsWeight){
            return false;
        }
        $measures = array($height, $length, $width);
        sort($measures);
        //measures.sort(function($a, $b){return $a-$b});
        if($measures[2]> $this->lsLongest){
            return false;
        }
        if($measures[1]> $this->lsMedian){
            return false;
        }
        if($measures[0]> $this->lsShortest){
            return false;
        }
        return true;
    }

    //start add xiaojing 2015-05-07
    function isEqualStandard($height,$length,$width,$weight)
    {
        $measures = array($height, $length, $width);
        sort($measures);
        if($weight <= $this->ssWeight && $measures[2] <= $this->ssLongest && $measures[1] <= $this->ssMedian && $measures[0] <= $this->ssShortest){
            return true;
        }else{
            return false;
        }
    }
    //start add xiaojing 2015-05-07

    function convertCurrency ($amountInRMB, $currency, $rate)
    {

        if($currency=="RMB"){
            return $this->parseFloat($amountInRMB,2);
        }
        else {
            return ($this->parseFloat($amountInRMB / $rate,2));
        }
    }

    function convertFromCurrency ($amountNotInRMB, $currency, $rate)
    {

        if($currency!="RMB"){
            return $this->parseFloat($amountNotInRMB,2);
        }
        else {
            return ($this->parseFloat($amountNotInRMB * $rate,2));
        }
    }

    function calcShipping()
    {
        if(!is_numeric($this->productHeight))
        {
            return "";
        }
        if(!is_numeric($this->productWidth))
        {
            return "";
        }
        if(!is_numeric($this->productLength))
        {
            return "";
        }

        if(!is_numeric($this->weightActual))
        {
            return "";
        }

        if(!is_numeric($this->freightCost))
        {
            return "";
        }
        //泡重
        $w = $this->productHeight * $this->productWidth * $this->productLength / 5000;
        $wa = $this->weightActual; //实重
        $shipping = 0;  //产品重量以泡重和实重 最大值 选取
        if($w > $wa)
        {
            //---//---echo "shipping 01";
            $shipping= $w * $this->freightCost;
        }
        else{
            //---//---echo "shipping =======";
            $shipping= $wa * $this->freightCost;
            ////---//---echo $this->freightCost;
        }
        /*Harley modify
        $this->finalShipping.set(convertCurrency(shipping,
          $this->calcCurrency,
          $this->currencyRate));
        return convertCurrency(shipping,
          $this->calcCurrency,
          $this->currencyRate);*/
        $this->finalShipping = $shipping;
        return $shipping;

    }
    //__set()方法用来设置私有属性

    function calcCost()
    {
        if(!is_numeric($this->cost))
        {
            return "";
        }
        /* harley modify
        Template.instance().finalCost.set(convertCurrency(Template.instance().cost.get(),
          Template.instance().calcCurrency.get(),
          Template.instance().currencyRate.get()));

        return convertCurrency(Template.instance().cost.get(),
          Template.instance().calcCurrency.get(),
          Template.instance().currencyRate.get());*/
        $this->finalCost = $this->cost;
        return $this->cost;


    }

    /*
    Template.commission.helpers({
      amzMarketSearchUrls: function(){
        return amzMarketSearchUrls;
    },
      currencyRate: function(){
        return Template.instance().currencyRate.get();
    },
      currencyRateDate: function(){
        return Template.instance().currencyRateDate.get();
    },
      commissionDefault:function(val){
        if(val==0.15){
            return "selected";
        }
        return "";
    },
      productLength: function(){
        return Template.instance().productLength.get();
    },
      productWidth: function(){
        return Template.instance().productWidth.get();
    },
      productHeight: function(){
        return Template.instance().productHeight.get();
    },
      commissionRate: function(){
        // return Template.instance().commission.get();
        return commissionRate;
    },
      freightCost: function(){
        if(isNaN(Template.instance().freightCost.get()))
        {
            return "";
        }
        return Template.instance().freightCost.get();
    },
        */
    function calcCurrency()
    {
        return $this->calcCurrency;
    }

    function calcPrice()
    {
        if(!is_numeric($this->priceInRMB))
        {
            return "";
        }

        $this->finalPrice = $this->convertCurrency($this->priceInRMB,
            $this->calcCurrency,
            $this->currencyRate);

        return $this->convertCurrency($this->priceInRMB,
            $this->calcCurrency,
            $this->currencyRate);


    }

    function calcCommission()
    {
        if(!is_numeric($this->priceInRMB))
        {
            return "";
        }
        $commission = $this->priceInRMB * $this->commission;
        ////---//---echo "commisssion is :$commission";


        $this->finalCommission = $this->convertCurrency($commission,
            $this->calcCurrency,
            $this->currencyRate);
        ////---//---echo "finalcommisssion is : $this->finalCommission";

        return $this->convertCurrency($commission,
            $this->calcCurrency,
            $this->currencyRate);


    }

    function calcMargin()
    {
        if(!is_numeric($this->finalPrice))
        {
            return "";
        }
        if(!is_numeric($this->finalCost))
        {
            return "";
        }
        if(!is_numeric($this->finalShipping))
        {
            return "";
        }
        if(!is_numeric($this->finalCommission))
        {
            return "";
        }
        if(!is_numeric($this->finalFBA))
        {
            return "";
        }
        /* Harley modify
        var $margin = $this->parseFloat($this->finalPrice )
      - $this->parseFloat($this->finalCost  )*/
        $margin = $this->parseFloat($this->finalPrice)

            - $this->convertCurrency($this->finalCost,
                $this->calcCurrency,
                $this->currencyRate)
            - $this->convertCurrency($this->finalShipping,
                $this->calcCurrency,
                $this->currencyRate)
            - $this->parseFloat($this->finalCommission)
            - $this->parseFloat($this->finalFBA) ;
        $this->finalMargin = $margin;
        return $this->parseFloat($margin,2);
    }

    function calcMarginPercent()
    {

        if(!is_numeric($this->finalMargin))
        {
            return "";
        }

        /* Harley modify
        return ($this->parseFloat(Template.instance().finalMargin.get())/$this->parseFloat(Template.instance().finalPrice.get())* 100,2);
        */

        //return $this->parseFloat($this->finalMargin)/$this->parseFloat($this->finalPrice * 100,2);

        //echo "$this->finalMargin";

        //echo "$this->finalPrice";

        $this->MarginPercent = $this->parseFloat(($this->parseFloat($this->finalMargin)/$this->parseFloat($this->finalPrice))* 100,2);

        return $this->parseFloat(($this->parseFloat($this->finalMargin)/$this->parseFloat($this->finalPrice))* 100,2);

    }

    function shootCodeBug($data){
        $data=mb_convert_encoding($data,'ISO-8859-1','gb2312');
        return mb_convert_encoding($data,'utf-8','GBK');
    }

    function getByURL($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch);

        curl_close($ch);

        return $output;
    }

    function updateCurrency()
    {
        $myurl = 'http://quote.eastmoney.com/center/forexlist.html';
        /*
       $ch = curl_init();
       $timeout = 10;

       curl_setopt ($ch, CURLOPT_URL, "http://quote.eastmoney.com/center/forexlist.html");
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
       $file_contents = curl_exec($ch);*/
        $file_contents = $this->getByURL($myurl);
        $file_contents = mb_convert_encoding($file_contents, 'utf-8', 'GBK,UTF-8,ASCII');
        //$file_contents = $this->shootCodeBug($file_contents);
        //curl_close($ch);

        //$file_contents = mb_convert_encoding($file_contents, 'utf-8', 'GBK,UTF-8,ASCII');

        //$html = file_get_contents($url);
        //echo $html;
        //echo "here!!";
        //echo $file_contents;

        $pos1 = strpos($file_contents,"yhpjboc");
        $pos2 = strpos($file_contents,"</div>",$pos1);
        $divcontent = substr($file_contents,$pos1 + 9,$pos2 -$pos1 - 9); //这个主要取 DIV 内容。
        //echo $divcontent;
        $trvalues  = explode("</tr>",$divcontent);
        //print_r($trvalues);

        $gbp = explode("<span class=\"digi\">",$trvalues[8]); //英镑
        $value1 = str_replace("</span></td><td>","",$gbp[1]);
        $value2 = str_replace("</span></td><td>","",$gbp[2]);
        $gpb_value = $this->parseFloat($value2)/$this->parseFloat($value1);
        //print_r($gbp);
        //echo "$value2 / $value1  = $gpb_value";
        $eur = explode("<span class=\"digi\">",$trvalues[7]); //英镑
        $value1 = str_replace("</span></td><td>","",$eur[1]);
        $value2 = str_replace("</span></td><td>","",$eur[2]);
        $eur_value = $this->parseFloat($value2)/$this->parseFloat($value1);
        //print_r($eur);
        //echo "$value2 / $value1  = $eur_value";

        $usd  =  explode("<span class=\"digi\">",$trvalues[24]); //美元
        $value1 = str_replace("</span></td><td>","",$usd[1]);
        $value2 = str_replace("</span></td><td>","",$usd[2]);
        $usd_value = $this->parseFloat($value2)/$this->parseFloat($value1);
        //print_r($usd);
        //echo "$value2 / $value1  = $usd_value";


        $currency= $this->calcCurrency;
        if($currency=="USD"){
            $this->currencyRate = $usd_value;
            //echo "$this->currencyRate   ==1 here";
            return $usd_value;
        }
        else if($currency=="RMB"){
            $this->currencyRate = 1;
            //echo "$this->currencyRate   ==2 here";
            return "1";
        }
        else if($currency=="GBP"){
            $this->currencyRate = $gpb_value;
            //echo "$this->currencyRate   ==3 here";
            return $gpb_value;
        }
        else if($currency=="EUR"){
            $this->currencyRate = $eur_value;
            //echo "$this->currencyRate   ==4 here";
            return $eur_value;
        }



        //$doc = phpQuery::newDocument($file_contents);

        //print $doc->html();
        /*
        $tr = $("#yhpjboc").find("tr");
        $td = $(tr[8]).find("td");
        $gbp = (parseFloat($(td[2]).text())/parseFloat($(td[1]).text()));
        $lastUpdate = new Date($(td[7]).text());

        $tr = $("#yhpjboc").find("tr");
        $td = $(tr[7]).find("td");
        $eur = (parseFloat($(td[2]).text())/parseFloat($(td[1]).text()));

        $tr = $("#yhpjboc").find("tr");
        $td = $(tr[24]).find("td");
        $usd = (parseFloat($(td[2]).text())/parseFloat($(td[1]).text()));*/


        /*
        $url = "http://quote.eastmoney.com/center/forexlist.html?from=baiduforex#3_6";
        $ch = curl_init();
        $timeout = 5;
        $this_header = array(
            "charset=UTF-8"
        );

        curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//在需要用户检测的网页里需要增加下面两行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
        $contents = curl_exec($ch);
        curl_close($ch);
        $contents = mb_convert_encoding($contents, 'utf-8', 'GBK,UTF-8,ASCII');

        //---//---
        echo "here!";

        echo $contents;


        /*

        var response = Meteor.http.call( 'GET','http://quote.eastmoney.com/center/forexlist.html?from=baiduforex#3_6');
        // console.log(response.content);
        $ = cheerio.load(response.content);

        var tr = $("#yhpjboc").find("tr");
        var td = $(tr[8]).find("td");
        var gbp = (parseFloat($(td[2]).text())/parseFloat($(td[1]).text()));
        var lastUpdate = new Date($(td[7]).text());

        tr = $("#yhpjboc").find("tr");
        td = $(tr[7]).find("td");
        var eur = (parseFloat($(td[2]).text())/parseFloat($(td[1]).text()));

        tr = $("#yhpjboc").find("tr");
        td = $(tr[24]).find("td");
        var usd = (parseFloat($(td[2]).text())/parseFloat($(td[1]).text()));


        var r = CurrencyRate.findOne();
        if(r==null){
            newRate = {'USD':usd,'GBP':gbp,'EUR':eur,'createdAt':lastUpdate};
          CurrencyRate.insert(newRate);
          return newRate;
      } else{
            if(lastUpdate> r.createdAt){
                newRate = {'USD':usd,'GBP':gbp,'EUR':eur,'createdAt':lastUpdate};
          CurrencyRate.remove({});
          CurrencyRate.insert(newRate);
          return newRate;
        }

        }
        return r;*/

    }

    function calcPriceOnMarginPercent()
    {

        //售价 ＝ (产品成本+头程运费+FBA费） / (1-毛利率－佣金比例)
        if(!is_numeric($this->finalCost)) //产品成本
        {
            //echo "1";
            return "";
        }
        if(!is_numeric($this->finalShipping)) //头程运费
        {
            //echo "2";
            return "";
        }
        if(!is_numeric($this->finalFBA)) //FBA费用
        {
            //echo "3";
            return "";
        }
        if(!is_numeric($this->MarginPercent)) //毛利率
        {
            //echo "4";
            return "";
        }
        if(!is_numeric($this->commission)) //佣金比例
        {
            //echo "5";
            return "";
        }

        //printf("值分别为：%f,%f,%f,%f,%f",$this->finalCost,$this->finalShipping,$this->finalFBA,$this->MarginPercent,$this->commission);

        /*$finalPrice = $this->parseFloat(
            (
                $this->convertCurrency($this->finalCost,
                    $this->calcCurrency,
                    $this->currencyRate)
                + $this->convertCurrency($this->finalShipping,
                    $this->calcCurrency,
                    $this->currencyRate)
                + $this->parseFloat($this->finalFBA))/
            (1-$this->MarginPercent-$this->commission)
        );*/

        $finalPrice = $this->parseFloat(($this->calcCost()+$this->calcShipping()+$this->calcFBA())/(1-($this->price/100)-$this->commission));

        //echo "售价: $finalPrice";

        $this->finalPrice = $finalPrice;

        return $this->parseFloat($finalPrice,2);

    }

    /**
     * 毛利 率推 算出 毛利额
     * @return float
     */
    function calcPriceOnMarginMargin(){
        return $this->parseFloat(($this->price/100) * $this->calcPriceOnMarginPercent());
    }

    function calcStatus()
    {
        if(!is_numeric($this->finalMargin))
        {
            return "";
        }
        if($this->parseFloat($this->finalMargin)>0){
            return "盈利";
        }
        else{
            return "亏损";
        }
    }

    function calcStatusClass()
    {
        if($this->parseFloat($this->finalMargin)>0){
            return "success";
        }
        else{
            return "danger";
        }
    }

    function priceCurrency()
    {
        return $this->priceCurrency;
    }

    function priceCurrencySymbol()
    {
        $pc = $this->priceCurrency;
        if($pc=="USD"){
            return "$";
        }
        else if($pc=="RMB"){
            return "￥";
        }
        else if($pc=="GBP"){
            return "£";
        }
        else if($pc=="EUR"){
            return "€";
        }
    }

    function priceBtnCurrencySymbol()
    {
        $pc = $this->priceCurrency;
        if($pc!="RMB"){
            return "￥";
        }
        if($pc=="RMB"){
            $m = $this->market;
            if($m=="US" ){
                return "$";
            }
            else if($m=="UK"){
                return "£";
            }
            else if($m=="DE" || $m=="FR"){
                return "€";
            }
        }
    }

    function calcCurrencyRate()
    {
        return $this->currencyRate;
    }

    function calcCurrencySymbol()
    {
        $currency= $this->calcCurrency;
        if($currency=="USD"){
            return "$";
        }
        else if($currency=="RMB"){
            return "￥";
        }
        else if($currency=="GBP"){
            return "£";
        }
        else if($currency=="EUR"){
            return "€";
        }
    }

    function calcBtnCurrencySymbol()
    {
        $currency = $this->calcCurrency;
        if($currency!="RMB"){
            return "￥";
        }
        if($currency=="RMB"){
            $m = $this->market;
            if($m=="US" ){
                return "$";
            }
            else if($m=="UK"){
                return "£";
            }
            else if($m=="DE" || $m=="FR"){
                return "€";
            }
        }
    }

    function calcStandSize(){
        if(!is_numeric($this->productHeight))
        {
            return "";
        }
        if(!is_numeric($this->productWidth))
        {

            return "";
        }
        if(!is_numeric($this->productLength))
        {

            return "";
        }
        if(!is_numeric($this->weightActual))
        {

            return "";
        }
        if(!is_numeric($this->freightCost))
        {

            return "";
        }

        $fbaFee = $this->orderHandling + $this->pickNPack ;

        $isStandSize = false;

        if($this->market=="US"){

            $vw = $this->productHeight * $this->productWidth * $this->productLength / InchToCm/InchToCm/InchToCm / 166;

            $wa = $this->parseFloat($this->weightActual * kgToPound); //harley modify 实际重单位转换

            /*var $weight = ($vw>$$wa)?$vw:$$wa;*/
            /*实际重 <=1 产品重=实际重 + 0.25
              实际重 >1 取大 + 0.25*/
            $weight = 0;
            if($wa + 0.25 <=1)
            {
                $weight = $wa;
            }
            else {

                $weight = ($vw>$wa)?$vw:$wa;
            }

            if($this->isStandard($this->productHeight,$this->productWidth,$this->productLength,$wa))
            {
                $isStandSize = true;
            }
            else if($this->isLargeStandard($this->productHeight,$this->productWidth,$this->productLength,$wa))
            {
                $isStandSize = true;
            }
            else{
                $isStandSize = false;
            }

            $this->standsize = $isStandSize?"标准":"OverSize";


            return $this->standsize;


        }
        else if($this->market=="UK"){

            $measures = [$this->productHeight , $this->productWidth , $this->productLength];
            sort($measures);

            $wa = $this->parseFloat($this->weightActual);
            $cost = 0;
            for($uu=0;$uu <count($this->ukWeightHandling);$uu++)
            {

                //echo "here!!!!!";

                if($this->ukWeightHandling[$uu]['length']<= $measures[2]){
                    continue;
                }
                if($this->ukWeightHandling[$uu]['height']<= $measures[1]){
                    continue;
                }
                if($this->ukWeightHandling[$uu]['width']<= $measures[0]){
                    continue;
                }


                if(($wa + $this->ukWeightHandling[$uu]['packingWeight']) < $this->ukWeightHandling[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->ukWeightHandling[$uu]['packingWeight'])  > $this->ukWeightHandling[$uu]['maxWeight']){
                    continue;
                }
                $cost = $this->ukWeightHandling[$uu]['cost'];

                break;
            }
            if($cost==0){
                $this->standsize = "OverSize";
                return $this->standsize;

            }
            $this->standsize = "标准";
            return $this->standsize;




        }
        else if($this->market=="DE"){

            $measures = array($this->productHeight , $this->productWidth , $this->productLength);
            sort($measures);

            $wa = $this->parseFloat($this->weightActual);
            $cost = 0;
            for($uu=0;$uu <count($this->deWeightHandling);$uu++)
            {
                if($this->deweightHandLing[$uu]['length']<= $measures[2]){
                    continue;
                }
                if($this->deweightHandLing[$uu]['height']<= $measures[1]){
                    continue;
                }
                if($this->deweightHandLing[$uu]['width']<= $measures[0]){
                    continue;
                }
                if(($wa + $this->deweightHandLing[$uu]['packingWeight']) < $this->deweightHandLing[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->deweightHandLing[$uu]['packingWeight'])  > $this->deweightHandLing[$uu]['maxWeight']){
                    continue;
                }
                $cost = $this->deweightHandLing[$uu]['cost'];
                break;
            }
            if($cost==0){
                $this->standsize = "OverSize";
                return $this->standsize;
            }

            $this->standsize = "标准";
            return $this->standsize;

        }
        else if($this->market=="FR"){

            $measures = array($this->productHeight , $this->productWidth , $this->productLength);
            sort($measures);

            $wa = $this->parseFloat($this->weightActual);
            $cost = 0;
            for($uu=0;$uu <count($this->frWeightHandling);$uu++)
            {
                if($this->frweightHandLing[$uu]['length']<= $measures[2]){
                    continue;
                }
                if($this->frweightHandLing[$uu]['height']<= $measures[1]){
                    continue;
                }
                if($this->frweightHandLing[$uu]['width']<= $measures[0]){
                    continue;
                }
                if(($wa + $this->frweightHandLing[$uu]['packingWeight']) < $this->frweightHandLing[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->frweightHandLing[$uu]['packingWeight'])  > $this->frweightHandLing[$uu]['maxWeight']){
                    continue;
                }
                $cost = $this->frweightHandLing[$uu]['cost'];
                break;
            }
            if($cost==0){
                $this->standsize = "OverSize";
                return $this->standsize;
            }

            $this->standsize = "标准";
            return $this->standsize;


        }
    }

    function calcFBA(){
        if(!is_numeric($this->productHeight))
        {
            return "";
        }
        if(!is_numeric($this->productWidth))
        {
            return "";
        }
        if(!is_numeric($this->productLength))
        {
            return "";
        }
        if(!is_numeric($this->weightActual))
        {
            return "";
        }


        $fbaFee = $this->orderHandling + $this->pickNPack ;
        //---//---echo "fbafee 01 here:$fbaFee";
        if($this->market=="US"){
            $vw = $this->productHeight * $this->productWidth * $this->productLength / InchToCm/InchToCm/InchToCm / 166;
            $wa = $this->parseFloat($this->weightActual * kgToPound); //harley modify 实际重单位转换
            /*var $weight = ($vw>$$wa)?$vw:$$wa;*/
            /*实际重 <=1 产品重=实际重 + 0.25
              实际重 >1 取大 + 0.25*/
            $weight = 0;

            if($wa + 0.25 <=1)
            {
                $weight = $wa;
            }
            else {

                $weight = ($vw>$wa)?$vw:$wa;
            }
            //---printf('productHeight,productWidth,productLength,InchToCm 每一个值分别是 ：%f,%f,%f,%f \n',$this->productHeight,$this->productWidth ,$this->productLength,InchToCm);
            //---printf('vw,wa,weight 每一个值分别是: %f,%f,%f \n',$vw,$wa,$weight);

            if($this->isStandard($this->productHeight,$this->productWidth,$this->productLength,$wa))
            {
                //---//---echo ' it is standard size\n';
                /*handlingWeight = $this->parseFloat($$wa) + $this->parseFloat(usPackingWeightSS); harley modify*/

                $handlingWeight = $this->parseFloat($weight) + $this->parseFloat($this->usPackingWeightSS);

                //console.log('fba' + fbaFee);
                //console.log('handlingWeight ' + handlingWeight);

                $fbaFee += $this->usWeightHandling["ss1"];

            }
            else if($this->isLargeStandard($this->productHeight,$this->productWidth,$this->productLength,$wa))
            {
                //---//---echo ' it is large standard size\n';

                //Harley
                $handlingWeight = $this->parseFloat($weight) + $this->parseFloat($this->usPackingWeightAbove1);
                //handlingWeight /= kgToPound; //harley added this line ，原因是没有按 b 来计算
                //---printf("weight %f",$this->parseFloat($weight));
                //---printf("usPackingWeightAbove1 %f",$this->parseFloat($this->usPackingWeightAbove1));
                //---printf("handlingWeight %f",$handlingWeight);

                //---printf('handlingWeight,weight,usPackingWeightAbove1 每一个值分别是 ：%f,%f,%f\n',$handlingWeight,$weight,$this->usPackingWeightAbove1);

                //statr edit xiaojing  2016-05-07
                /*if($handlingWeight < 1) { // harley modify }(1 /kgToPound )){
                    //---//---echo '跳转到这了 1 这';
                    $fbaFee += $this->parseFloat($this->usWeightHandling["ls1"]);
                }
                else if ($handlingWeight < 2){// harley modify} /kgToPound )){
                    //---//---echo '跳转到这了 2 这';
                    $fbaFee += $this->parseFloat($this->usWeightHandling["ls1to2"]);
                }
                else{
                    //---//---echo '跳转到这了 else 这';
                    $remain = $this->parseInt(($handlingWeight -(2 /kgToPound )) / ((1 /kgToPound )));
                    //console.log(remain);
                    $fbaFee += $this->parseFloat($this->usWeightHandling["ls1to2"]) + $remain * $this->parseFloat($this->usWeightHandlingAbove2);

                }*/
                if($this->isEqualStandard($this->productHeight,$this->productWidth,$this->productLength,$wa)){
                    $fbaFee +=  0.50;
                }else{
                    if($handlingWeight < 1) { // harley modify }(1 /kgToPound )){
                        //---//---echo '跳转到这了 1 这';
                        $fbaFee += $this->parseFloat($this->usWeightHandling["ls1"]);
                    }
                    else if ($handlingWeight < 2){// harley modify} /kgToPound )){
                        //---//---echo '跳转到这了 2 这';
                        $fbaFee += $this->parseFloat($this->usWeightHandling["ls1to2"]);
                    }
                    else{
                        $fbaFee += $this->parseFloat($this->usWeightHandling["ls1to2"]) + $this->parseFloat($this->usWeightHandlingAbove2);
                    }
                }
                //statr edit xiaojing  2016-05-07
            }
            else{
                //console.log(' it is too large');
                return "暂不支持Oversize";
            }

            $this->finalFBA = $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);

            // //---printf("fbaFee %f",$fbaFee);

            return $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);

        }
        else if($this->market=="UK"){

            //console.log('in uk');
            $measures = [$this->productHeight , $this->productWidth , $this->productLength];
            sort($measures);
            //measures.sort(function(a, b){return a-b});
            $wa = $this->parseFloat($this->weightActual);
            $cost = 0;
            for($uu=0;$uu <count($this->ukWeightHandling);$uu++)
            {

                //console.log('measures[2]');
                //console.log(measures[2]);
                //console.log('ukWeightHandling[uu].length');
                //console.log(ukWeightHandling[uu].length);

                //start endit xiaojing  2016-05-07
                /* if($this->ukWeightHandling[$uu]['length']<= $measures[2]){
                     continue;
                 }
                 if($this->ukWeightHandling[$uu]['height']<= $measures[1]){
                     continue;
                 }
                 if($this->ukWeightHandling[$uu]['width']<= $measures[0]){
                     continue;
                 }*/
                if($this->ukWeightHandling[$uu]['length']< $measures[2]){
                    continue;
                }
                if($this->ukWeightHandling[$uu]['height']< $measures[1]){
                    continue;
                }
                if($this->ukWeightHandling[$uu]['width']< $measures[0]){
                    continue;
                }
                //end endit xiaojing  2016-05-07

                //console.log(($wa));
                //console.log( ukWeightHandling[uu].packingWeight);
                //console.log(ukWeightHandling[uu].minWeight + " to "+ukWeightHandling[uu].maxWeight);


                if(($wa + $this->ukWeightHandling[$uu]['packingWeight']) < $this->ukWeightHandling[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->ukWeightHandling[$uu]['packingWeight'])  > $this->ukWeightHandling[$uu]['maxWeight']){
                    continue;
                }

                $cost = $this->ukWeightHandling[$uu]['cost'];


                //console.log('uu cost');
                //console.log(ukWeightHandling[uu].cost);
                break;
            }
            if($cost==0){
                return "暂不支持Oversize";
            }
            //console.log($cost);
            $fbaFee += $cost;

            $this->finalFBA = $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);
            return $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);
        }
        else if($this->market=="DE"){
            //console.log('in uk');
            $measures = array($this->productHeight , $this->productWidth , $this->productLength);
            sort($measures);
            //measures.sort(function(a, b){return a-b});
            $wa = $this->parseFloat($this->weightActual);
            $cost = 0;

            for($uu=0;$uu <count($this->deWeightHandling);$uu++)
            {
                //start endit xiaojing  2016-05-07
                /*if($this->deweightHandLing[$uu]['length']<= $measures[2]){
                    continue;
                }
                if($this->deweightHandLing[$uu]['height']<= $measures[1]){
                    continue;
                }
                if($this->deweightHandLing[$uu]['width']<= $measures[0]){
                    continue;
                }
                if(($wa + $this->deweightHandLing[$uu]['packingWeight']) < $this->deweightHandLing[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->deweightHandLing[$uu]['packingWeight'])  > $this->deweightHandLing[$uu]['maxWeight']){
                    continue;
                }
                $cost = $this->deweightHandLing[$uu]['cost'];*/

                if($this->deWeightHandling[$uu]['length']< $measures[2]){
                    continue;
                }
                if($this->deWeightHandling[$uu]['height']< $measures[1]){
                    continue;
                }
                if($this->deWeightHandling[$uu]['width']< $measures[0]){
                    continue;
                }
                if(($wa + $this->deWeightHandling[$uu]['packingWeight']) < $this->deWeightHandling[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->deWeightHandling[$uu]['packingWeight'])  > $this->deWeightHandling[$uu]['maxWeight']){
                    continue;
                }
                $cost = $this->deWeightHandling[$uu]['cost'];
                //end endit xiaojing  2016-05-07

                break;
            }
            if($cost==0){
                return "暂不支持Oversize";
            }
            //console.log(cost);
            $fbaFee += $cost;

            $this->finalFBA = $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);
            return $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);

        }
        else if($this->market=="FR"){
            //console.log('in uk');
            $measures = array($this->productHeight , $this->productWidth , $this->productLength);
            sort($measures);
            //measures.sort(function(a, b){return a-b});
            $wa = $this->parseFloat($this->weightActual);
            $cost = 0;

            for($uu=0;$uu <count($this->frWeightHandling);$uu++)
            {
                //start endit xiaojing  2016-05-07
                /*  if($this->frweightHandLing[$uu]['length']<= $measures[2]){
                      continue;
                  }
                  if($this->frweightHandLing[$uu]['height']<= $measures[1]){
                      continue;
                  }
                  if($this->frweightHandLing[$uu]['width']<= $measures[0]){
                      continue;
                  }
                  if(($wa + $this->frweightHandLing[$uu]['packingWeight']) < $this->frweightHandLing[$uu]['minWeight']){
                      continue;
                  }
                  if(($wa + $this->frweightHandLing[$uu]['packingWeight'])  > $this->frweightHandLing[$uu]['maxWeight']){
                      continue;
                  }
                  $cost = $this->frweightHandLing[$uu]['cost'];*/
                if($this->frWeightHandling[$uu]['length']< $measures[2]){
                    continue;
                }
                if($this->frWeightHandling[$uu]['height']< $measures[1]){
                    continue;
                }
                if($this->frWeightHandling[$uu]['width']< $measures[0]){
                    continue;
                }
                if(($wa + $this->frWeightHandling[$uu]['packingWeight']) < $this->frWeightHandling[$uu]['minWeight']){
                    continue;
                }
                if(($wa + $this->frWeightHandling[$uu]['packingWeight'])  > $this->frWeightHandling[$uu]['maxWeight']){
                    continue;
                }
                $cost = $this->frWeightHandling[$uu]['cost'];
                //end endit xiaojing  2016-05-07
                break;
            }
            if($cost==0){
                return "暂不支持Oversize";
            }
            //console.log(cost);
            $fbaFee += $cost;

            $this->finalFBA = $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);
            return $this->convertFromCurrency($fbaFee,
                $this->calcCurrency,
                $this->currencyRate);


        }
    }

    function parseFloat($value,$fix = 0)
    {
        if($fix == 0)
        {
            ////---printf("value is %f",$value);
            ////---printf("floatval is %f",floatval($value));
            return floatval($value);
        }
        $tmp  = floatval($value);
        ////---printf("tmp1 %f",$tmp);

        $tmp = round($value,$fix);

        ////---printf("tmp2 %f",$tmp);
        return $tmp;

    }

    function parseInt($value)
    {
        return intval($value);
    }

    function price()
    {
        return $this->price;
    }

    public function __get($property_name)
    {
        //---//---echo "在直接获取私有属性值的时候，自动调用了这个__get()方法<br>";
        if(isset($this->$property_name))
        {
            return($this->$property_name);
        }
        else
        {
            return(NULL);
        }
    }

    public function __set($property_name, $value)
    {
        ////---//---echo "在直接设置私有属性值的时候，自动调用了这个__set()方法为私有属性赋值<br>";
        $this->$property_name = $value;

        if($property_name =="market")
        {
            ////---//---echo "market here!";
            $this->orderHandling = $this->marketOrderHandling[$value];
            $this->pickNPack = $this->marketpickNpack[$value];

            if($value=="US"){
                $this->priceCurrency = "USD";
                //template.currencyRate.set(rates.USD.toFixed(4));
                $this->calcCurrency = "USD";
            }
            else if($value=="UK"){
                $this->priceCurrency = "GBP";
                //template.currencyRate.set(rates.GBP.toFixed(4));
                $this->calcCurrency = "GBP";
            }
            else {
                $this->priceCurrency ="EUR";
                //template.currencyRate.set(rates.EUR.toFixed(4));
                $this->calcCurrency = "EUR";
            }


        }

        if($property_name =="price"){
            $pcurrency = $this->priceCurrency;
            if($pcurrency=="RMB"){
                $this->priceInRMB = $value;
            }
            else{
                //calc price in RMB
                $this->priceInRMB = $value * $this->currencyRate;
            }

        }
    }

    /*
        'keyup #txtPrice': function (event,template) {

        if(isNaN(event.target.value)){
            return;
        }
        template.price.set(event.target.value);
        var $price = template.price.get();
        var $pcurrency = template.priceCurrency.get();
        if(pcurrency=="RMB"){
            template.priceInRMB.set(price);
        }
        else{
            //calc price in RMB
            template.priceInRMB.set(price* template.currencyRate.get());
        }
    },
        'keyup #txtFreightCost':function (event,template) {

        if(isNaN(event.target.value)){
            return;
        }
        template.freightCost.set(event.target.value);
    },
        'click #refreshRate': function(e,template){
        $(e.target).text("更新中。。。");
        Meteor.call('updateCurrency',function(err,result){
            rates = result;
            var $m = $("#amzMarket option:selected" ).val();
            template.orderHandling.set(marketOrderHandling[m]);
            template.pickNPack.set(marketpickNpack[m]);
            if(m=="US"){
                template.priceCurrency.set("USD");
                template.currencyRate.set(rates.USD.toFixed(4));
                template.calcCurrency.set("USD");
            }
            else if(m=="UK"){
                template.priceCurrency.set("GBP");
                template.currencyRate.set(rates.GBP.toFixed(4));
                template.calcCurrency.set("GBP");
            }
            else {
                template.priceCurrency.set("EUR");
                template.currencyRate.set(rates.EUR.toFixed(4));
                template.calcCurrency.set("EUR");
            }
            $(e.target).text("更新");

            template.currencyRateDate.set(moment(rates.createdAt).format('YYYY MM DD h:mm a'));
            var $price = template.price.get();
            if(isNaN(price)){
                return;
            }
            var $pcurrency = template.priceCurrency.get();
            if(pcurrency=="RMB"){
                template.priceInRMB.set(price);
            }
            else{
                //calc price in RMB
                template.priceInRMB.set(price* template.currencyRate.get());
            }

        });
    },
      'change #amzCommissionRate': function(e,template){
        template.commission.set($("#amzCommissionRate option:selected" ).val());
    },
      'change #amzMarket': function(e,template){
        template.market.set($("#amzMarket option:selected" ).val());
        var $m = $("#amzMarket option:selected" ).val();
        console.log(m);
        template.orderHandling.set(marketOrderHandling[m]);
        template.pickNPack.set(marketpickNpack[m]);
        if(m=="US"){
            template.priceCurrency.set("USD");
            template.currencyRate.set(rates.USD.toFixed(4));
            template.calcCurrency.set("USD");
        }
        else if(m=="UK"){
            template.priceCurrency.set("GBP");
            template.currencyRate.set(rates.GBP.toFixed(4));
            template.calcCurrency.set("GBP");
        }
        else {
            template.priceCurrency.set("EUR");
            template.currencyRate.set(rates.EUR.toFixed(4));
            template.calcCurrency.set("EUR");
        }


        template.currencyRateDate.set(moment(rates.createdAt).format('YYYY MM DD h:mm a'));
        var $price = template.price.get();
        if(isNaN(price)){
            return;
        }
        var $pcurrency = template.priceCurrency.get();
        if(pcurrency=="RMB"){
            template.priceInRMB.set(price);
        }
        else{
            //calc price in RMB
            template.priceInRMB.set(price* template.currencyRate.get());
        }

    },
      'click #btnChangePriceCurrency': function(e,template){
        e.preventDefault();
        var $m = $("#amzMarket option:selected" ).val();
        var $price = template.price.get();
        if(template.priceCurrency.get()=="RMB")
        {
            if(m=="US"){
                template.priceCurrency.set("USD");
            }
            else if(m=="UK"){
                template.priceCurrency.set("GBP");
            }
            else {
                template.priceCurrency.set("USD");
            }
        }
        else{
            template.priceCurrency.set("RMB");
        }
        var $pcurrency = template.priceCurrency.get();
        if(pcurrency=="RMB"){
            template.priceInRMB.set(price);
            template.price.set(price * template.currencyRate.get());
        }
        else{
            //calc price in RMB
            template.priceInRMB.set(price* template.currencyRate.get());
            template.price.set(price / template.currencyRate.get());
        }

    },
      'click #btnChangeCalcCurrency': function(e,template){
        e.preventDefault();
        var $m = $("#amzMarket option:selected" ).val();
        if(template.calcCurrency.get()=="RMB")
        {
            if(m=="US"){
                template.calcCurrency.set("USD");
            }
            else if(m=="UK"){
                template.calcCurrency.set("GBP");
            }
            else {
                template.calcCurrency.set("EUR");
            }
        }
        else{
            template.calcCurrency.set("RMB");
        }
    }
    });

    }
    */
}